﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI;
using YArchitech.Revit;

namespace YArchitech.HYSpace
{
	[Transaction(TransactionMode.Manual)]
	public class CreatSpaceByRoom : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			UIApplication application = commandData.Application;
			Document document = application.ActiveUIDocument.Document;
			Transaction transaction = new Transaction(document, "创建空间");
			transaction.Start();
			string pathName = "C:\\Program Files\\Hongye\\HYRME2.0\\Data\\WallData.txt";
			YJKRevitTools.SetInstanceParameter(application, pathName, "墙体附加数据", new List<string>
			{
				"基础房间ID"
			}, (BuiltInCategory)(-2003600), false, BuiltInParameterGroup.INVALID);
			IEnumerable<Element> elementByCategory = YJKElementFilter.GetElementByCategory(document, BuiltInCategory.OST_MEPSpaces);
			IList<Space> list = new List<Space>();
			foreach (Element element in elementByCategory)
			{
				Space space = element as Space;
				if (space != null)
				{
					list.Add(space);
				}
			}
			foreach (Element element2 in ((IEnumerable<Element>)YJKElementFilter.GetElementByCategory(document, BuiltInCategory.OST_Rooms)))
			{
				Room room = element2 as Room;
				if (!YJKRevitTools.isExsitSpace(room, list))
				{
					YJKRevitTools.CreatSpace(document, room);
				}
			}
			IList<Level> list2 = new List<Level>();
			foreach (KeyValuePair<Room, Transform> keyValuePair in this.GetLinkAllRoom(application))
			{
				if (!YJKRevitTools.isExsitSpace(keyValuePair.Key, list))
				{
					YJKRevitTools.CreatSpace(document, keyValuePair.Key, keyValuePair.Value, ref list2);
				}
			}
			transaction.Commit();
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public Dictionary<Room, Transform> GetLinkAllRoom(UIApplication uiApp)
		{
			Dictionary<Room, Transform> dictionary = new Dictionary<Room, Transform>();
			Document document = uiApp.ActiveUIDocument.Document;
			FilteredElementCollector elementsOfType = YJKElementFilter.GetElementsOfType(document, typeof(Instance), BuiltInCategory.OST_RvtLinks);
			IList<string> linkFilePaths = YJKRevitTools.GetLinkFilePaths(document);
			foreach (Element element in elementsOfType)
			{
				Instance instance = element as Instance;
				if (instance != null)
				{
					Transform totalTransform = instance.GetTotalTransform();
					(document.GetElement(instance.GetTypeId()) as RevitLinkType).GetParameter(BuiltInParameter.WALL_ATTR_ROOM_BOUNDING).Set(1);
					string value = instance.Name.Substring(0, instance.Name.IndexOf(".rvt"));
					string text = "";
					foreach (string text2 in linkFilePaths)
					{
						if (text2.Contains(value))
						{
							text = text2;
						}
					}
					FilteredElementCollector filteredElementCollector = new FilteredElementCollector(uiApp.Application.OpenDocumentFile(text));
					filteredElementCollector.OfCategory(BuiltInCategory.OST_Rooms);
					foreach (Element element2 in filteredElementCollector)
					{
						Room room = element2 as Room;
						if (room.Location != null)
						{
							dictionary.Add(room, totalTransform);
						}
					}
				}
			}
			return dictionary;
		}
	}
}
